import bb.cascades 1.3
import bb.cascades.datamanager 1.2
import bb.WebImageView 1.4
import bb.data 1.0
import "lib"
import "controls"

Sheet {
    id: sheet
    
    peekEnabled: false
    
    property alias url: dataSource.source
    Page {
        actionBarVisibility: ChromeVisibility.Compact   
        
        Container {
            id: viewSlides
            
            gestureHandlers: [
                DoubleTapHandler {
                    onDoubleTapped: {
                        sheet.close();
                    }
                }
            ]
            
            layout: DockLayout {
            }
            
            ListView {
                id: view
                
                verticalAlignment: VerticalAlignment.Fill
                horizontalAlignment: HorizontalAlignment.Fill
                
                preferredHeight: com.screenHeight
                
                dataModel: GroupDataModel {
                    id: theDataModel
                    grouping: ItemGrouping.None
                    sortingKeys: ["index"]
                }
                
                onTriggered: {
                }
                
                layout: StackListLayout {
                    orientation: LayoutOrientation.LeftToRight
                    headerMode: ListHeaderMode.None
                }
                
                snapMode: SnapMode.LeadingEdge
                flickMode: FlickMode.SingleItem
                scrollIndicatorMode: ScrollIndicatorMode.ProportionalBar
                stickToEdgePolicy: ListViewStickToEdgePolicy.BeginningThenEnd
                layoutProperties: StackLayoutProperties {
                }
                
                listItemComponents: [
                    ListItemComponent {
                        type: "item"
                        ViewSliderItem {
                            image: ListItemData.image
                            title: ListItemData.title
                            desc: ListItemData.description
                            itemIndex: ListItemData.itemIndex
                            
                            onIsShowChanged: {
                                
                            }
                        }
                    }
                ]
            }
            
            attachedObjects: [
                Common {
                    id: com
                },
                DataSource {
                  id: dataSource
                  onSourceChanged: {
                      console.log(url)
                      dataSource.load()
                  }
                  onDataLoaded: {
                      var results = [];
                      var slides = data.body.slides
                        for(var i=0;i<slides.length;i++){
                           var item = slides[i]
                           item["screenWidth"] = com.screenWidth
                           item["screenHeight"] = com.screenHeight
                           item["itemIndex"] = qsTr("%1/%2").arg(slides.length-i).arg(slides.length)
                           results.push(item)
                        }
                        theDataModel.insertList(results)
                  }
                }       
            ]
        }
        
        actions: [
            ActionItem {
                title: qsTr("Close")
                ActionBar.placement: ActionBarPlacement.Signature
                onTriggered: {
                    sheet.close();
                }
            }
        ]
    }
}
